package jwcb.javapjframework.ms_template.service.controller;

import jwcb.javapjframework.common.domain.domain.dto.LoginUserDto;
import jwcb.javapjframework.common.security.domain.dto.TokenDto;
import jwcb.javapjframework.common.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;

@RestController
@RequestMapping("/test/token")
public class TokenTestController {

    @Autowired
    private TokenService tokenService; // 假设上述代码在TokenService中

    @GetMapping
    public String testTokenOperations() {
        StringBuilder result = new StringBuilder();
        
        // 1. 创建模拟登录用户
        LoginUserDto loginUser = new LoginUserDto();
        loginUser.setUserId(10001L);
        loginUser.setUserFrom("web");
        loginUser.setUserName("testUser");
        result.append("创建初始用户: ").append(loginUser).append("\n---\n");

        // 2. 创建Token
        TokenDto tokenDto = tokenService.createToken(loginUser);
        String accessToken = tokenDto.getAccessToken();
        result.append("生成Token: ").append(accessToken).append("\n---\n");
        
        // 3. 通过Token获取用户
        LoginUserDto cachedUser = tokenService.getLoginUserDto(accessToken);
        boolean match = cachedUser != null 
                && Objects.equals(cachedUser.getUserId(), loginUser.getUserId())
                && Objects.equals(cachedUser.getUserKey(), loginUser.getUserKey());
        result.append("通过Token查询用户: ").append(cachedUser)
              .append("\n匹配结果: ").append(match ? "成功" : "失败").append("\n---\n");
        
        // 4. 修改用户信息并更新缓存
        if (cachedUser != null) {
            cachedUser.setUserName("updatedUser");
            tokenService.setLoginUser(cachedUser);
            
            LoginUserDto updatedUser = tokenService.getLoginUserDto(accessToken);
            boolean updateSuccess = updatedUser != null 
                    && "updatedUser".equals(updatedUser.getUserName());
            result.append("更新用户信息后查询: ").append(updatedUser)
                  .append("\n更新验证: ").append(updateSuccess ? "成功" : "失败").append("\n---\n");
        }

        // 5. 删除当前Token
        tokenService.removeLoginStatus(accessToken);
        LoginUserDto afterDelete = tokenService.getLoginUserDto(accessToken);
        result.append("删除Token后查询: ").append(afterDelete)
              .append("\n验证: ").append(afterDelete == null ? "删除成功" : "删除失败").append("\n---\n");
        
        // 6. 测试管理员删除
        // 创建两个同用户不同来源的Token
        LoginUserDto user1 = new LoginUserDto();
        user1.setUserId(10002L);
        user1.setUserFrom("app");
        TokenDto token1 = tokenService.createToken(user1);

        LoginUserDto user2 = new LoginUserDto();
        user2.setUserId(10002L);
        user2.setUserFrom("app");
        TokenDto token2 = tokenService.createToken(user2);
        
        // 管理员删除操作
        tokenService.removeLoginStatus(10002L, "app");
        
        // 验证删除结果
        boolean token1Exists = tokenService.getLoginUserDto(token1.getAccessToken()) != null;
        boolean token2Exists = tokenService.getLoginUserDto(token2.getAccessToken()) != null;
        result.append("管理员删除后验证: ")
              .append("\nToken1存在: ").append(token1Exists)
              .append("\nToken2存在: ").append(token2Exists)
              .append("\n总体结果: ").append((!token1Exists && !token2Exists) ? "成功" : "失败");
        
        return result.toString();
    }
}